package com.yzt.redpack.dialog;

import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.yzt.redpack.RedPkgDetailActivity;
import com.yzt.plugin_widget.RoundImageView;
import com.yzt.redpack.R;
import com.yzt.zhuju.RXingUtils;
import com.yzt.zhuju.dialog.CenterBaseDialog;
import com.yzt.zhuju.event.RxBus;
import com.yzt.zhuju.event.im.RedPkgEvent;
import com.yzt.zhuju.network.AresSubscriber;
import com.yzt.zhuju.network.biz.BizLayer;
import com.yzt.zhuju.network.entity.dynamic.RedPkgDetailBean;
import com.yzt.zhuju.widget.widget.ImageLoadManger;

/**
 * 作者：bobwang
 * 创建时间：2023-10-12 21:27:43
 * 描述：打开红包
 */
public class GameRedOpenDialog extends CenterBaseDialog {

    private RoundImageView imgAvatar;
    private TextView tv_nickname;
    private TextView tv_content;
    private LinearLayout ll_coin_number;
    private TextView tv_coin_number;
    private TextView tv_detail;
    private LinearLayout llRedBg;

    private String redPacketId;

    private RedPkgDetailBean redPkgInfoBean;

    private String mTargetId;

    @Override
    public int getLayoutRes() {
        return R.layout.dialog_open_red_pkg;
    }

    @Override
    public void bindView(View v) {
        redPkgInfoBean = getArguments().getParcelable("extra_data");
        mTargetId = getArguments().getString("extra_targetId");
        imgAvatar = findViewById(R.id.img_avatar);
        tv_nickname = findViewById(R.id.tv_nickname);
        tv_content = findViewById(R.id.tv_content);
        ll_coin_number = findViewById(R.id.ll_coin_number);
        tv_coin_number = findViewById(R.id.tv_coin_number);
        tv_detail = findViewById(R.id.tv_detail);
        llRedBg = findViewById(R.id.ll_red_bg);

        // 关闭窗口
        findViewById(R.id.img_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismissAllowingStateLoss();
            }
        });

        // 查看记录
        findViewById(R.id.tv_detail).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), RedPkgDetailActivity.class);
                intent.putExtra("extra_id", redPacketId);
                startActivity(intent);
                dismissAllowingStateLoss();
            }
        });

        // 打开红包
        findViewById(R.id.view_open).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (redPkgInfoBean == null || !redPkgInfoBean.isCanOpen()) {
                    return;
                }
                openRedPacket();
            }
        });

        setData(redPkgInfoBean);
    }

    public void setData(RedPkgDetailBean redPkgInfo) {
        redPacketId = redPkgInfoBean.getRedPacketId();
        ImageLoadManger.loadIv(mActivity, redPkgInfo.getAvatar(), imgAvatar);
        tv_nickname.setText(redPkgInfo.getTitle());
        tv_content.setText(redPkgInfo.getDsc());
        if (redPkgInfo.isCanOpen()) {
            llRedBg.setBackground(ContextCompat.getDrawable(mActivity, R.mipmap.icon_red_pkg_bg_open));
        } else {
            llRedBg.setBackground(ContextCompat.getDrawable(mActivity, R.mipmap.icon_red_pkg_bg));
        }
    }

    /**
     * 打开红包 TODO
     */
    private void openRedPacket() {
        BizLayer.getInstance().getDynamicModule().openRedPacketInfo(redPacketId, new AresSubscriber<RedPkgDetailBean>() {
            @Override
            protected void onSuccess(RedPkgDetailBean redPkgInfo) {
                llRedBg.setBackground(ContextCompat.getDrawable(mActivity, R.mipmap.icon_red_pkg_bg));
                tv_content.setVisibility(View.GONE);
                ll_coin_number.setVisibility(View.VISIBLE);
                tv_detail.setVisibility(View.GONE);
                redPkgInfoBean.setCanOpen(false);
                if (redPkgInfo.getUserList() != null && !redPkgInfo.getUserList().isEmpty()) {
                    tv_coin_number.setText(redPkgInfo.getUserList().get(0).getCandyCoin());
                }
                RedPkgEvent event = new RedPkgEvent();
                event.setSendTxt("你领取了" + redPkgInfoBean.getSendUserName() + "的");
                event.setReceiveTxt(RXingUtils.getPersonDetail().getUserBasic().getNickName() + "领取了你的");
                event.setTargetUid(mTargetId);
                RxBus.getDefault().post(event);
            }
        });
    }
}
